package entity

import (
	"github.com/jinzhu/gorm"
)

// 药品类别
type DrugCategory struct {
	Id   int    `gorm:"primary_key;AUTO_INCREMENT"`
	Name string `gorm:"type:char(128);unique"`
	Sort int
}

func (t *DrugCategory) TableName() string {
	return "drug_category"
}

// 风险累及系统——器官
type DrugRisk struct {
	Id   int    `gorm:"primary_key;AUTO_INCREMENT"`
	Name string `gorm:"type:char(128);unique"`
	Sort int
}

func (t *DrugRisk) TableName() string {
	return "drug_risk"
}

// 药品报告
type DrugReport struct {
	Id            uint         `gorm:"primary_key;AUTO_INCREMENT"`
	Number        string       `gorm:"type:varchar(24);unique"`
	Title         string       `gorm:"size=255"`
	Category      DrugCategory `gorm:"ForeignKey:CategoryRefer;AssociationForeignKey:Id"`
	CategoryRefer int
	RiskRefer     string `gorm:"size=255"`
	ReportedAt    int64
	UserId        uint   `gorm:"index"`
	Username      string `gorm:"size=255"`
	Admin         Admin  `gorm:"ForeignKey:AdminId;AssociationForeignKey:Id"`
	AdminId       int
	Unit          string `gorm:"size=255"`
	Remark        string `gorm:"text"`
	Province      string `gorm:"type:varchar(16);index"`
	City          string `gorm:"type:varchar(16);index"`
	County        string `gorm:"type:varchar(16);index"`
	Address       string `gorm:"type:varchar(255)"`
	Phone         string `gorm:"type:char(11)"`
	Status        int    `gorm:"index"`
	CheckedAt     int64
	Comment       string      `gorm:"size=255"`
	DrugAnnexes   []DrugAnnex `gorm:"ForeignKey:Pid"`
	Notice        int
}

func (t *DrugReport) TableName() string {
	return "drug_report"
}

// 附件表
type DrugAnnex struct {
	Id  uint   `gorm:"primary_key;AUTO_INCREMENT"`
	Pid uint   `gorm:"index"` // 关联 DrugReport
	Src string `gorm:"size=255"`
}

func (t *DrugAnnex) TableName() string {
	return "drug_annex"
}

// 附件表
type DeviceRiskDescAnnex struct {
	Id        uint   `gorm:"primary_key;AUTO_INCREMENT"`
	Pid       uint   `gorm:"index"` // 关联 DeviceReport
	Src       string `gorm:"size=255"`
	Type      int
	AnnexType int // 0 文本 1 图片 2 word 3 pdf
}

func (t *DeviceRiskDescAnnex) TableName() string {
	return "device_risk_desc_annex"
}

type DeviceRiskAnalysisAnnex struct {
	Id        uint   `gorm:"primary_key;AUTO_INCREMENT"`
	Pid       uint   `gorm:"index"` // 关联 DeviceReport
	Src       string `gorm:"size=255"`
	Type      int
	AnnexType int // 0 文本 1 图片 2 word 3 pdf
}

func (t *DeviceRiskAnalysisAnnex) TableName() string {
	return "device_risk_analysis_annex"
}

// 风险描述类别
type DeviceRiskDesc struct {
	Id   int    `gorm:"primary_key;AUTO_INCREMENT"`
	Name string `gorm:"type:char(128);unique"`
	Sort int
}

func (t *DeviceRiskDesc) TableName() string {
	return "device_risk_desc"
}

// 风险分析类别
type DeviceRiskAnalysis struct {
	Id   int    `gorm:"primary_key;AUTO_INCREMENT"`
	Name string `gorm:"type:char(128);unique"`
	Sort int
}

func (t *DeviceRiskAnalysis) TableName() string {
	return "device_risk_analysis"
}

// 医疗器械
type DeviceReport struct {
	Id            uint   `gorm:"primary_key;AUTO_INCREMENT"`
	Number        string `gorm:"type:varchar(24);unique"`
	CompanyName   string `gorm:"type:varchar(255)"`
	ProduceName   string `gorm:"type:varchar(255)"`
	ProduceNumber string `gorm:"type:varchar(255)"`
	ProduceId     string `gorm:"type:varchar(255)"`
	ReportedAt    int64
	// User User `gorm:"ForeignKey:UserId;AssociationForeignKey:Id"`
	UserId              uint   `gorm:"index"`
	Username            string `gorm:"size=255"`
	Admin               Admin  `gorm:"ForeignKey:AdminId;AssociationForeignKey:Id"`
	AdminId             int
	Unit                string `gorm:"size=255"`
	Province            string `gorm:"type:varchar(16);index"`
	City                string `gorm:"type:varchar(16);index"`
	County              string `gorm:"type:varchar(16);index"`
	Address             string `gorm:"type:varchar(255)"`
	Phone               string `gorm:"type:varchar(11)"`
	Comment             string `gorm:"size=255"`
	Status              int    `gorm:"index"`
	CheckedAt           int64
	RiskDescRefer       string                    `gorm:"type:varchar(255)"` // 风险描述
	RiskAnalysisRefer   string                    `gorm:"type:varchar(255)"` // 风险分析
	RiskDescAnnexes     []DeviceRiskDescAnnex     `gorm:"ForeignKey:Pid"`
	RiskAnalysisAnnexes []DeviceRiskAnalysisAnnex `gorm:"ForeignKey:Pid"`
	Notice              int
}

func (t *DeviceReport) TableName() string {
	return "device_report"
}

// 报告日志
type ReportLog struct {
	UserId       uint         `gorm:"type:int(11);primary_key;index"`
	Pid          uint         `gorm:"type:int(11);primary_key;index"`
	Type         int          `gorm:"type:int(10);primary_key;index"`
	DrugReport   DrugReport   `gorm:"ForeignKey:Pid;AssociationForeignKey:Id"`
	DeviceReport DeviceReport `gorm:"ForeignKey:Pid;AssociationForeignKey:Id"`
	DrugAnnexes  []DrugAnnex  `gorm:"ForeignKey:Pid;AssociationForeignKey:Pid"`
	CreatedAt    int64        `gorm:"index"`
}

// 讨论
type ReportDiscussCategory struct {
	Pid       uint   `gorm:"type:int(10);unsigned;primary_key"`
	Comment   string `gorm:"type:varchar(255)"`
	CreatedAt string `gorm:"type:datetime;index"`
	Status    int    `gorm:"index"`
	Count     int
	Province  string `gorm:"type:varchar(16);index"`
	City      string `gorm:"type:varchar(16);index"`
	County    string `gorm:"type:varchar(16);index"`
}

func (m *ReportDiscussCategory) TableName() string {
	return "report_discuss_category"
}

func (m *ReportDiscussCategory) BeforeSave(scope *gorm.Scope) (err error) {
	_ = scope.SetColumn("count", gorm.Expr("count + ?", m.Count))
	return nil
}

func (m *ReportDiscussCategory) BeforeUpdate(scope *gorm.Scope) (err error) {
	_ = scope.SetColumn("count", gorm.Expr("count + ?", m.Count))
	return nil
}

type ReportDiscuss struct {
	Id                 uint                 `gorm:"auto"`
	Pid                uint                 `gorm:"index"`
	Number             string               `gorm:"type:varchar(64);index"`
	Message            string               `gorm:"type:varchar(255)"`
	User               User                 `gorm:"ForeignKey:UserId;AssociationForeignKey:Id"`
	UserId             uint                 `gorm:"index"`
	CreatedAt          string               `gorm:"type:datetime;index"`
	Province           string               `gorm:"type:varchar(16);index"`
	City               string               `gorm:"type:varchar(16);index"`
	County             string               `gorm:"type:varchar(16);index"`
	Address            string               `gorm:"type:varchar(255)"`
	ReportDiscussAnnex []DeportDiscussAnnex `gorm:"ForeignKey:Pid"`
}

func (m *ReportDiscuss) TableName() string {
	return "report_discuss"
}

type DeportDiscussAnnex struct {
	Id  uint   `gorm:"primary_key;AUTO_INCREMENT"`
	Pid uint   `gorm:"index"` // 关联 DeportDiscussAnnex
	Src string `gorm:"size=255"`
}

func (m *DeportDiscussAnnex) TableName() string {
	return "report_discuss_annex"
}
